#include "promenidelegate.h"
#include "promenieditor.h"

#include <QtGui>
#include <QPainter>


QString PromeniDelegate::displayText(const QVariant &value, const QLocale &locale) const
{
    QString promeni = value.toString();
    if (promeni.isEmpty())
        return QStyledItemDelegate::displayText(QString(), locale);
    QStringList promeniList = promeni.split(",");
    return promeniList.join(", ");
}

QSize PromeniDelegate::sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const
{
    return QSize(200, 24);
}

QWidget *PromeniDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    Q_UNUSED(option); Q_UNUSED(index);
    PromeniEditor *editor = new PromeniEditor(parent);
    connect(editor, SIGNAL(editingFinished()),
            this, SLOT(commitAndCloseEditor()));
    return editor;
}

void PromeniDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    PromeniEditor *edt = qobject_cast<PromeniEditor*>( editor );
    edt->setPromeni( index.data().toString() );
}

void PromeniDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    PromeniEditor *edt = qobject_cast<PromeniEditor*>( editor );
    model->setData(index, edt->getPromeni());
}


void PromeniDelegate::commitAndCloseEditor()
{
    PromeniEditor *editor = qobject_cast<PromeniEditor*>(sender());
    emit commitData(editor);
    emit closeEditor(editor);
}


PromeniDelegate::PromeniDelegate(QWidget *parent) :
    QStyledItemDelegate(parent)
{
}

PromeniDelegate::~PromeniDelegate()
{
}
